package wufei_database;

import java.sql.*;
import java.util.*;

// class             | class_ca  | title           | attriname         | attrivalue  

public class FeiDB {

	Connection conn;
	Statement stmt;
	//Statement stmt;
	public FeiDB(){
		try{
			Class.forName("com.mysql.jdbc.Driver").newInstance();
			conn = DriverManager.getConnection(
					"jdbc:mysql://pardosa04.cs.washington.edu/trwiki",
					"clzhang", "kylin");
			stmt = conn.createStatement();
			
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	public ArrayList<FeiInfobox> selectByName(String name){
		name = name.replace("'","''");
		ArrayList<FeiInfobox> result = new ArrayList<FeiInfobox> ();
		try{
			
			ResultSet res;
			Statement stmt = conn.createStatement();
			res = stmt.executeQuery("select * from infobox20080724_ca where title='"+name+"'");
			while (res.next()) {
				FeiInfobox fib = new FeiInfobox();
				fib.title = name;
				fib.template_name = res.getString("class_ca");
				fib.attriname = res.getString("attriname");
				fib.attrivalue = res.getString("attrivalue");
				result.add(fib);
			}
			res.close();
			stmt.close();
		}catch(Exception e){
			e.printStackTrace();
		}
		return result;
	}
	public static void main(String[] args) {
		FeiDB fb = new FeiDB();
		for(int i=0;i<1000;i++){
		ArrayList<FeiInfobox> fblist = fb.selectByName("Actrius");
		System.out.println(fblist);
		}
		
		
	}
	
	public static void mai2(String[] args) {
		try {
			Connection conn;
			Statement stmt;
			ResultSet res;
			Class.forName("com.mysql.jdbc.Driver").newInstance();

			conn = DriverManager.getConnection(
					"jdbc:mysql://pardosa04.cs.washington.edu/trwiki",
					"clzhang", "kylin");

			stmt = conn.createStatement();
			res = stmt
					.executeQuery("select * from infobox20080724_ca where title='Albert Einstein'");

			while (res.next()) {
				String name = res.getString("class");
				System.out.println(name);
			}
			res.close();

		} catch (Exception ex) {
			System.out.println("Error : " + ex.toString());
		}
	}
}